package com.yellowpage.ndc.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;
import com.yellowpage.ndc.data.ProdCustomer;
import com.yellowpage.ndc.data.ProdGroup;
import com.yellowpage.ndc.service.ProdClassService;
import com.yellowpage.ndc.service.ProdCustomerService;
import com.yellowpage.ndc.service.ProdGroupService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.struts.BaseAction;

public class ProdOrderAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_MEMU = "menu";

	
	private Configuration configuration;
	
	

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<ProdGroup> prodGroupList = new ArrayList<ProdGroup>();
	private List<Map> resultList = new ArrayList<Map>();
	private List<Map> classScend = new ArrayList<Map>();
	
	private String prodClassStr;
	private List<Map> List = new ArrayList<Map>();
	private ProdGroup prodGroup;
	private ProdGroupService prodGroupService;
	private ProdCustomerService prodCustomerService;
	private ProdClassService prodClassService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_prodClassCode1;
	private String query_prodClassName1;
	private String query_prodClassCode2;
	private String query_prodClassName2;
	private String query_groupId;
	private String query_name1;
	private String query_name2;
	private String query_subname1;
	private String query_subname2;
	private String query_subname3;
	private String query_subname4;
	private String query_tel;
	private String query_telExt;
	private String query_faxFlag;
	private String query_sb;
	private String query_listOrder;
	private String query_regionCode;
	private String query_regionName;
	private String query_cityCode;
	private String query_cityName;
	private String query_destrictCode;
	private String query_destrictName;
	private String query_areaCode;
	private String query_addr;
	private String query_zip;
	private String query_pyAddr;
	private String query_engAddr;
	private String query_poix;
	private String query_poiy;
	private String query_custStatus;
	private String query_custTypeCode;
	private String query_classMainId;
	private String query_orgId;
	private String query_url;
	private String query_weibo;
	private String query_weiboName;
	private String query_weixin;
	private String query_yixin;
	private String query_logo;
	private String query_safeStatus;
	private String query_bxName;
	private String query_pyName;
	private String query_engName;
	private String query_spName;
	private String query_prodClassId;
	private String query_modifyTime;
	private String query_modifyEmpId;
	private String query_inputTime;
	private String query_inputEmpId;
	private String query_id;
	private String query_inputEmpCode;
	private String query_modifyEmpCode;
	private String query_code1;
	private String query_code2;
	private String[] selectedUpdate;
	
	private static final int BUFFER_SIZE = 16 * 1024;
	private String uploadContentType;
	private String savePath;
	private File upload;
	private String uploadFileName;
	
	private String class_code;
	private String class_name;
	
	
	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.prodGroup = prodGroupService.getProdGroup(Long
					.parseLong(selectedId));
		}
	}

	
	
	public String queryByPage() {
		
		Map queryMap = new HashMap();
		queryMap.put("code1","%"+query_prodClassCode1+"%");
		queryMap.put("code2","%"+query_prodClassCode2+"%");
		resultList = prodClassService.getProdClassTree(queryMap);

		prodClassStr = "";
		for (Map map : resultList) {
			prodClassStr +="d.add("+map.get("classid")+",0,'"+map.get("name")+"','prodOrderAction!orderByPage.action?query_prodClassCode1="+map.get("code")+"','','mainframe');";

			
			List<Map> scendlist = (List<Map>) map.get("scendClass");
			
			for (Map valer : scendlist) {
				prodClassStr +="d.add("+valer.get("CODE2")+","+map.get("classid")+",'"+valer.get("NAME2")+"','prodOrderAction!orderByPage.action?query_prodClassCode2="+valer.get("CODE2")+"','','mainframe');";
			}
		}
		return RESULT_MEMU;
			
		}


	
	public String orderByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("prodClassCode1",query_prodClassCode1);
		queryMap.put("prodClassName1", query_prodClassName1);
		queryMap.put("prodClassCode2", query_prodClassCode2);
		queryMap.put("prodClassName2", query_prodClassName2);
		queryMap.put("groupId", query_groupId);
		queryMap.put("name1", query_name1);
		queryMap.put("name2", query_name2);
		queryMap.put("subname1", query_subname1);
		queryMap.put("subname2", query_subname2);
		queryMap.put("subname3", query_subname3);
		queryMap.put("subname4", query_subname4);
		queryMap.put("tel", query_tel);
		queryMap.put("telExt", query_telExt);
		queryMap.put("faxFlag", query_faxFlag);
		queryMap.put("sb", query_sb);
		queryMap.put("listOrder", query_listOrder);
		queryMap.put("regionCode", query_regionCode);
		queryMap.put("regionName", query_regionName);
		queryMap.put("cityCode", query_cityCode);
		queryMap.put("cityName", query_cityName);
		queryMap.put("destrictCode", query_destrictCode);
		queryMap.put("destrictName", query_destrictName);
		queryMap.put("areaCode", query_areaCode);
		queryMap.put("addr", query_addr);
		queryMap.put("zip", query_zip);
		queryMap.put("pyAddr", query_pyAddr);
		queryMap.put("engAddr", query_engAddr);
		queryMap.put("poix", query_poix);
		queryMap.put("poiy", query_poiy);
		queryMap.put("custStatus", query_custStatus);
		queryMap.put("custTypeCode", query_custTypeCode);
		queryMap.put("classMainId", query_classMainId);
		queryMap.put("orgId", query_orgId);
		queryMap.put("url", query_url);
		queryMap.put("weibo", query_weibo);
		queryMap.put("weiboName", query_weiboName);
		queryMap.put("weixin", query_weixin);
		queryMap.put("yixin", query_yixin);
		queryMap.put("logo", query_logo);
		queryMap.put("safeStatus", query_safeStatus);
		queryMap.put("bxName", query_bxName);
		queryMap.put("pyName", query_pyName);
		queryMap.put("engName", query_engName);
		queryMap.put("spName", query_spName);
		queryMap.put("prodClassId", query_prodClassId);
		queryMap.put("modifyTime", query_modifyTime);
		queryMap.put("modifyEmpId", query_modifyEmpId);
		queryMap.put("inputTime", query_inputTime);
		queryMap.put("inputEmpId", query_inputEmpId);
		queryMap.put("id", query_id);
		queryMap.put("inputEmpCode", query_inputEmpCode);
		queryMap.put("modifyEmpCode", query_modifyEmpCode);

		resultList = prodGroupService
				.ProdGroupOrderByPage(queryMap, pageRender);

		//setPageRenderToRequestNoCount();
		setPageRenderToRequest();
		return RESULT_QUERY;
	}

	public String toOrder() {
		return RESULT_QUERY;
	}
	


	

	public String toup() {
		Long id = new Long(selectedId);
		prodGroup = prodGroupService.getProdGroup(id);
		Integer listOrder = prodGroup.getListOrder();
		prodGroup.setListOrder(listOrder - 1);
		Integer listOrder2 = new Integer(listOrder - 1);
		resultList = prodGroupService.getProdGrouplistorder(listOrder2);
		if(resultList.size()==0){
			prodGroupService.updateProdGroup(prodGroup);
			
		}else{
		Long id2 = Long.parseLong(resultList.get(0).get("ID").toString());
		prodGroupService.updateProdGroup(prodGroup);
		prodGroup = prodGroupService.getProdGroup(id2);
		prodGroup.setListOrder(listOrder);
		prodGroupService.updateProdGroup(prodGroup);}
		message = "修改成功!";
		return orderByPage();
	}
	
	public String todown() {
		Long id = new Long(selectedId);
		prodGroup = prodGroupService.getProdGroup(id);
		Integer listOrder = prodGroup.getListOrder();
		prodGroup.setListOrder(listOrder + 1);
		Integer listOrder2 = new Integer(listOrder + 1);
		resultList = prodGroupService.getProdGrouplistorder(listOrder2);
		if(resultList.size()==0){
			prodGroupService.updateProdGroup(prodGroup);
			
		}else{
		Long id2 = Long.parseLong(resultList.get(0).get("ID").toString());
		prodGroupService.updateProdGroup(prodGroup);
		prodGroup = prodGroupService.getProdGroup(id2);
		prodGroup.setListOrder(listOrder);
		prodGroupService.updateProdGroup(prodGroup);}

		message = "修改成功!";
		return orderByPage();
	}
	
	

	
	
	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setProdGroup(ProdGroup prodGroup) {
		this.prodGroup = prodGroup;
	}

	public ProdGroup getProdGroup() {
		return prodGroup;
	}

	public void setProdGroupService(ProdGroupService prodGroupService) {
		this.prodGroupService = prodGroupService;
	}

	public List<ProdGroup> getProdGroupList() {
		return prodGroupList;
	}

	public void setProdGroupList(List<ProdGroup> prodGroupList) {
		this.prodGroupList = prodGroupList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public List<Map> getClassScend() {
		return classScend;
	}



	public void setClassScend(List<Map> classScend) {
		this.classScend = classScend;
	}



	public String getQuery_prodClassCode1() {
		return this.query_prodClassCode1;
	}

	public void setQuery_prodClassCode1(String prodClassCode1) {
		this.query_prodClassCode1 = prodClassCode1;
	}

	public String getQuery_prodClassName1() {
		return this.query_prodClassName1;
	}

	public void setQuery_prodClassName1(String prodClassName1) {
		this.query_prodClassName1 = prodClassName1;
	}

	public String getQuery_prodClassCode2() {
		return this.query_prodClassCode2;
	}

	public void setQuery_prodClassCode2(String prodClassCode2) {
		this.query_prodClassCode2 = prodClassCode2;
	}

	public String getQuery_prodClassName2() {
		return this.query_prodClassName2;
	}

	public void setQuery_prodClassName2(String prodClassName2) {
		this.query_prodClassName2 = prodClassName2;
	}

	public String getQuery_groupId() {
		return this.query_groupId;
	}

	public void setQuery_groupId(String groupId) {
		this.query_groupId = groupId;
	}

	public String getQuery_name1() {
		return this.query_name1;
	}

	public void setQuery_name1(String name1) {
		this.query_name1 = name1;
	}

	public String getQuery_name2() {
		return this.query_name2;
	}

	public void setQuery_name2(String name2) {
		this.query_name2 = name2;
	}

	public String getQuery_subname1() {
		return this.query_subname1;
	}

	public void setQuery_subname1(String subname1) {
		this.query_subname1 = subname1;
	}

	public String getQuery_subname2() {
		return this.query_subname2;
	}

	public void setQuery_subname2(String subname2) {
		this.query_subname2 = subname2;
	}

	public String getQuery_subname3() {
		return this.query_subname3;
	}

	public void setQuery_subname3(String subname3) {
		this.query_subname3 = subname3;
	}

	public String getQuery_subname4() {
		return this.query_subname4;
	}

	public void setQuery_subname4(String subname4) {
		this.query_subname4 = subname4;
	}

	public String getQuery_tel() {
		return this.query_tel;
	}

	public void setQuery_tel(String tel) {
		this.query_tel = tel;
	}

	public String getQuery_telExt() {
		return this.query_telExt;
	}

	public void setQuery_telExt(String telExt) {
		this.query_telExt = telExt;
	}

	public String getQuery_faxFlag() {
		return this.query_faxFlag;
	}

	public void setQuery_faxFlag(String faxFlag) {
		this.query_faxFlag = faxFlag;
	}

	public String getQuery_sb() {
		return this.query_sb;
	}

	public void setQuery_sb(String sb) {
		this.query_sb = sb;
	}

	public String getQuery_listOrder() {
		return this.query_listOrder;
	}

	public void setQuery_listOrder(String listOrder) {
		this.query_listOrder = listOrder;
	}

	public String getQuery_regionCode() {
		return this.query_regionCode;
	}

	public void setQuery_regionCode(String regionCode) {
		this.query_regionCode = regionCode;
	}

	public String getQuery_regionName() {
		return this.query_regionName;
	}

	public void setQuery_regionName(String regionName) {
		this.query_regionName = regionName;
	}

	public String getQuery_cityCode() {
		return this.query_cityCode;
	}

	public void setQuery_cityCode(String cityCode) {
		this.query_cityCode = cityCode;
	}

	public String getQuery_cityName() {
		return this.query_cityName;
	}

	public void setQuery_cityName(String cityName) {
		this.query_cityName = cityName;
	}

	public String getQuery_destrictCode() {
		return this.query_destrictCode;
	}

	public void setQuery_destrictCode(String destrictCode) {
		this.query_destrictCode = destrictCode;
	}

	public String getQuery_destrictName() {
		return this.query_destrictName;
	}

	public void setQuery_destrictName(String destrictName) {
		this.query_destrictName = destrictName;
	}

	public String getQuery_areaCode() {
		return this.query_areaCode;
	}

	public void setQuery_areaCode(String areaCode) {
		this.query_areaCode = areaCode;
	}

	public String getQuery_addr() {
		return this.query_addr;
	}

	public void setQuery_addr(String addr) {
		this.query_addr = addr;
	}

	public String getQuery_zip() {
		return this.query_zip;
	}

	public void setQuery_zip(String zip) {
		this.query_zip = zip;
	}

	public String getQuery_pyAddr() {
		return this.query_pyAddr;
	}

	public void setQuery_pyAddr(String pyAddr) {
		this.query_pyAddr = pyAddr;
	}

	public String getQuery_engAddr() {
		return this.query_engAddr;
	}

	public void setQuery_engAddr(String engAddr) {
		this.query_engAddr = engAddr;
	}

	public String getQuery_poix() {
		return this.query_poix;
	}

	public void setQuery_poix(String poix) {
		this.query_poix = poix;
	}

	public String getQuery_poiy() {
		return this.query_poiy;
	}

	public void setQuery_poiy(String poiy) {
		this.query_poiy = poiy;
	}

	public String getQuery_custStatus() {
		return this.query_custStatus;
	}

	public void setQuery_custStatus(String custStatus) {
		this.query_custStatus = custStatus;
	}

	public String getQuery_custTypeCode() {
		return this.query_custTypeCode;
	}

	public void setQuery_custTypeCode(String custTypeCode) {
		this.query_custTypeCode = custTypeCode;
	}

	public String getQuery_classMainId() {
		return this.query_classMainId;
	}

	public void setQuery_classMainId(String classMainId) {
		this.query_classMainId = classMainId;
	}

	public String getQuery_orgId() {
		return this.query_orgId;
	}

	public void setQuery_orgId(String orgId) {
		this.query_orgId = orgId;
	}

	public String getQuery_url() {
		return this.query_url;
	}

	public void setQuery_url(String url) {
		this.query_url = url;
	}

	public String getQuery_weibo() {
		return this.query_weibo;
	}

	public void setQuery_weibo(String weibo) {
		this.query_weibo = weibo;
	}

	public String getQuery_weixin() {
		return this.query_weixin;
	}

	public void setQuery_weixin(String weixin) {
		this.query_weixin = weixin;
	}

	public String getQuery_yixin() {
		return this.query_yixin;
	}

	public void setQuery_yixin(String yixin) {
		this.query_yixin = yixin;
	}

	public String getQuery_logo() {
		return this.query_logo;
	}

	public void setQuery_logo(String logo) {
		this.query_logo = logo;
	}

	public String getQuery_safeStatus() {
		return this.query_safeStatus;
	}

	public void setQuery_safeStatus(String safeStatus) {
		this.query_safeStatus = safeStatus;
	}

	public String getQuery_bxName() {
		return this.query_bxName;
	}

	public void setQuery_bxName(String bxName) {
		this.query_bxName = bxName;
	}

	public String getQuery_pyName() {
		return this.query_pyName;
	}

	public void setQuery_pyName(String pyName) {
		this.query_pyName = pyName;
	}

	public String getQuery_engName() {
		return this.query_engName;
	}

	public void setQuery_engName(String engName) {
		this.query_engName = engName;
	}

	public String getQuery_spName() {
		return this.query_spName;
	}

	public void setQuery_spName(String spName) {
		this.query_spName = spName;
	}

	public String getQuery_prodClassId() {
		return this.query_prodClassId;
	}

	public void setQuery_prodClassId(String prodClassId) {
		this.query_prodClassId = prodClassId;
	}

	public String getQuery_modifyTime() {
		return this.query_modifyTime;
	}

	public void setQuery_modifyTime(String modifyTime) {
		this.query_modifyTime = modifyTime;
	}

	public String getQuery_modifyEmpId() {
		return this.query_modifyEmpId;
	}

	public void setQuery_modifyEmpId(String modifyEmpId) {
		this.query_modifyEmpId = modifyEmpId;
	}

	public String getQuery_inputTime() {
		return this.query_inputTime;
	}

	public void setQuery_inputTime(String inputTime) {
		this.query_inputTime = inputTime;
	}

	public String getQuery_inputEmpId() {
		return this.query_inputEmpId;
	}

	public void setQuery_inputEmpId(String inputEmpId) {
		this.query_inputEmpId = inputEmpId;
	}

	public String getQuery_id() {
		return this.query_id;
	}

	public void setQuery_id(String id) {
		this.query_id = id;
	}

	public String getQuery_inputEmpCode() {
		return query_inputEmpCode;
	}

	public void setQuery_inputEmpCode(String queryInputEmpCode) {
		query_inputEmpCode = queryInputEmpCode;
	}

	public String getQuery_modifyEmpCode() {
		return query_modifyEmpCode;
	}

	public void setQuery_modifyEmpCode(String queryModifyEmpCode) {
		query_modifyEmpCode = queryModifyEmpCode;
	}

	public String getQuery_weiboName() {
		return query_weiboName;
	}

	public void setQuery_weiboName(String queryWeiboName) {
		query_weiboName = queryWeiboName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	
	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String[] getSelectedUpdate() {
		return selectedUpdate;
	}

	public void setSelectedUpdate(String[] selectedUpdate) {
		this.selectedUpdate = selectedUpdate;
	}

	public ProdCustomerService getProdCustomerService() {
		return prodCustomerService;
	}

	public void setProdCustomerService(ProdCustomerService prodCustomerService) {
		this.prodCustomerService = prodCustomerService;
	}

	public ProdGroupService getProdGroupService() {
		return prodGroupService;
	}

	public ProdClassService getProdClassService() {
		return prodClassService;
	}

	public void setProdClassService(ProdClassService prodClassService) {
		this.prodClassService = prodClassService;
	}

	public String getClass_code() {
		return class_code;
	}

	public void setClass_code(String class_code) {
		this.class_code = class_code;
	}

	public String getClass_name() {
		return class_name;
	}

	public void setClass_name(String class_name) {
		this.class_name = class_name;
	}

	public List<Map> getList() {
		return List;
	}

	public void setList(List<Map> list) {
		List = list;
	}



	public String getProdClassStr() {
		return prodClassStr;
	}



	public void setProdClassStr(String prodClassStr) {
		this.prodClassStr = prodClassStr;
	}



	public String getQuery_code1() {
		return query_code1;
	}



	public void setQuery_code1(String query_code1) {
		this.query_code1 = query_code1;
	}



	public String getQuery_code2() {
		return query_code2;
	}



	public void setQuery_code2(String query_code2) {
		this.query_code2 = query_code2;
	}








}
